if (window.plus) {
  plusReady();
} else {
  document.addEventListener('plusready', plusReady, false);
}

// Web
$(function () {
    if (AGENT_CLIENT.WEB) {
        plusReady();
    } else if (AGENT_CLIENT.APP) {
        $(".main").css("padding-bottom", "2px");
    }
});

// 初始化方法
function plusReady() {
    var _thisPage = null, _uploadPage = null, _user = null, _myScroll = null;

    var mylifePage = function () {
        this.init();
    };

    mylifePage.prototype = {
        init: function () {
            this.loadData();
        },
        //加载
        loadData: function () {
            //添加fastclick
            //window.addEventListener("load", function(){ FastClick.attach(document.body); }, false);
            //_uploadPage = new uploadPage();

            _user = getLocalStorage(STORAGE_MANAGER.user);
            if (!_user) {
                this.loadContent(null);
                this.bindEvent(null);
            }
            $.ajax({
                url: CONFIG_MANAGER.RequstUrl + "v1/member/" + _user.id,
                type: "GET",
                async: true,
                headers: {"token": _user.token},
                beforeSend: function () {
                    $.showWaiting();
                }
            }).done(function (data) {
                _thisPage.loadContent(data);
                _thisPage.bindEvent(data);
            }).always(function (data) {
                $.closeWaiting();
            }).fail(function () {
                _thisPage.loadContent(null);
                _thisPage.bindEvent(null);
            });
        },
        loadContent: function (data) {
            var html = [], src = '';
            //头像
            if (data && data.member_id > 0) {
                // 更新用户缓存
                _user["coin"] = data.coin;
                setLocalStorage(STORAGE_MANAGER.user, _user);

                html.push('<div class="wdshtbbg">');
                html.push('<div class="dptxbg">');
                if (data.avatar) {
                    src = CONFIG_MANAGER.imgeUrl + data.avatar;
                    //src = getImageUrl(data.avatar,330,330);
                } else {
                    src = "../../images/xfxtu3.jpg";
                }
                html.push('<p id="avatar" class="dptxtu"><img src="' + src + '" /></p>');
                html.push('<p id="nickname" class="dpmingche"><span>' + data.nickname + '</span>');
                html.push('<i class="iconfont icon-erweima"></i>');
                html.push('</p>');
                html.push('</div>');
                html.push('</div>');
            } else {
                html.push('<div class="wdshtbbg">');
                html.push('<div class="dptxbg">');
                html.push('<p id="avatar" class="dptxtu"><span class="wdshwdltx"><img src="../../images/wdltx.png"/><br>登录/注册</span></p>');
                html.push('<p id="nickname" class="dpmingche huangse">欢迎来到如此生活</p>');
                html.push('</div>');
                html.push('</div>');
            }
            //钱包和关注
            if (data && data.member_id > 0) {
                html.push('<div class="muokuaibg mbtopbottom mtop300">');
                html.push('<div class="height mbbottom"></div>');
                html.push('<div class="mkyis mbbottom">');
                html.push('<p class="wdshxj mbright hover-style">');
                html.push('现金金额:  <b>' + data.cash + '</b><br><em>冻结：' + data.frozen_cash + '</em>');
                html.push('</p>');
                html.push('<p class="wdshxj1">');
                html.push('<span class="mbbottom hover-style">我的钱包:  <b>' + data.money + '</b><em>冻结：' + data.frozen_money + '</em></span>');
                html.push('<span class="hover-style">我的星币:  <b>' + data.coin + '</b><em>冻结：' + data.frozen_coin + '</em></span>');
                html.push('</p>');
                html.push('</div>');
                html.push('<div class="mkyix">');
                html.push('<p id="focus_goods" class="wdsbbb mbright hover-style"><b>' + data.focus_goods + '</b><br>关注的宝贝</p>');
                html.push('<p id="focus_shop" class="wdsbbb mbright hover-style"><b>' + data.focus_shop + '</b><br>关注的店铺</p>');
                html.push('<p id="trace" class="wdsbbb hover-style"><b>' + data.focus_goods + '</b><br>我的足迹</p>');
                html.push('</div>');
                html.push('</div>');
            } else {
                html.push('<div class="muokuaibg mbtopbottom mtop300">');
                html.push('<div class="height mbbottom"></div>');
                html.push('<div class="mkyis mbbottom">');
                html.push('<p class="wdshxj mbright hover-style">');
                html.push('现金金额:  <b>0</b> <br><em>冻结：0</em>');
                html.push('</p>');
                html.push('<p class="wdshxj1">');
                html.push('<span class="mbbottom hover-style">我的钱包:  <b>0</b><em>冻结：0</em></span>');
                html.push('<span class="hover-style">我的星币:  <b>0</b><em>冻结：0</em></span>');
                html.push('</p>');
                html.push('</div>');
                html.push('<div class="mkyix">');
                html.push('<p id="focus_goods" class="wdsbbb mbright hover-style"><b>0</b><br>关注的宝贝</p>');
                html.push('<p id="focus_shop" class="wdsbbb mbright hover-style"><b>0</b><br>关注的店铺</p>');
                html.push('<p id="trace" class="wdsbbb hover-style"><b>0</b><br>我的足迹</p>');
                html.push('</div>');
                html.push('</div>');
            }
            //订单分类个数
            //data.status = 200,data.non_payment=2,data.pending_orders=1;
            if (data && data.member_id > 0) {
                html.push('<div class="navbg mbtopbottom">');
                html.push('<h2 class="mkbiaoti mbbottom">');
                html.push('<em class="wdshtb"><b><img src="../../images/wdshtb1.png"></b>我的订单</em>');
                html.push('<span id="orders" class="wdshyb"><i class="iconfont icon-jiantou2"></i><i class="huizi">查看全部已购买订单</i></span>');
                html.push('</h2>');

                html.push('<ul class="nav nav-tubiao">');
                //待付款
                html.push('<li name="liorders" tip="non_payment">');
                if (data.non_payment > 0) {
                    html.push('<p class="dfhsl">' + data.non_payment + '</p>');
                }
                html.push('<i class="iconfont icon-youhuiquan"></i><span>待付款</span></li>');
                //待发货
                html.push('<li name="liorders" tip=""><i class="iconfont icon-wuliu"></i><span>待发货</span></li>');
                //待评价
                html.push('<li name="liorders" tip="pending_orders">');
                if (data.pending_orders > 0) {
                    html.push('<p class="dfhsl">' + data.pending_orders + '</p>');
                }
                html.push('<i class="iconfont icon-daipingjia"></i><span>待评价</span></li>');
                //退款售后
                html.push('<li name="liorders" tip=""><i class="iconfont icon-gouwu"></i><span>退款/售后</span></li>');
                html.push('</ul>');
                html.push('</div>');
            } else {
                html.push('<div class="navbg mbtopbottom">');
                html.push('<h2 class="mkbiaoti mbbottom">');
                html.push('<em class="wdshtb"><b><img src="../../images/wdshtb1.png"></b>我的订单</em>');
                html.push('<span id="orders" class="wdshyb"><i class="iconfont icon-jiantou2"></i><i class="huizi">查看全部已购买订单</i></span>');
                html.push('</h2>');
                html.push('<ul class="nav nav-tubiao">');
                html.push('<li name="liorders" tip="non_payment"><i class="iconfont icon-youhuiquan"></i><span>待付款</span></li>');
                html.push('<li name="liorders" tip=""><i class="iconfont icon-wuliu"></i><span>待发货</span></li>');
                html.push('<li name="liorders" tip="pending_orders"><i class="iconfont icon-daipingjia"></i><span>待评价</span></li>');
                html.push('<li name="liorders" tip=""><i class="iconfont icon-gouwu"></i><span>退款/售后</span></li>');
                html.push('</ul>');
                html.push('</div>');
            }
            //我的如此生活
            if (data && data.member_id > 0) {
                html.push('<div class="navbg mbtopbottom">');
                html.push('<h2 class="mkbiaoti mbbottom">');
                html.push('<em class="wdshtb"><b><img src="../../images/wdshtb2.png"></b>我的如此生活</em>');
                html.push('<span class="wdshyb"><i class="huizi"></i><a href="#"><i class="iconfont icon-jiantou2"></i></a></span>');
                html.push('</h2>');
                html.push('<ul class="nav nav-tubiao">');
                html.push('<li id="coinlist"><i class="iconfont icon-xingxing"></i><span>领星币</span></li>');
                html.push('<li id="myShow"><i class="iconfont icon-xiangji"></i><span>我的星粉秀</span></li>');
                html.push('<li id="myCoupon"><i class="iconfont icon-youhuiquan"></i><span>我的优惠券</span></li>');

                html.push('<li id="myMsg">');
                if (data.unread_messages > 0) {
                    html.push('<p class="dfhsl">' + data.unread_messages + '</p>');
                }
                html.push('<i class="iconfont icon-xiaoxi"></i><span>我的消息</span></a></li>');
                html.push('</ul>');
                html.push('</div>');
            } else {
                html.push('<div class="navbg mbtopbottom">');
                html.push('<h2 class="mkbiaoti mbbottom">');
                html.push('<em class="wdshtb"><b><img src="../../images/wdshtb2.png"></b>我的如此生活</em>');
                html.push('<span class="wdshyb"><i class="huizi"></i><a href="#"><i class="iconfont icon-jiantou2"></i></a></span>');
                html.push('</h2>');
                html.push('<ul class="nav nav-tubiao">');
                html.push('<li id="coinlist"><i class="iconfont icon-xingxing"></i><span>领星币</span></li>');
                html.push('<li id="myShow"><i class="iconfont icon-xiangji"></i><span>我的星粉秀</span></li>');
                html.push('<li id="myCoupon"><i class="iconfont icon-youhuiquan"></i><span>我的优惠券</span></li>');
                html.push('<li id="myMsg"><i class="iconfont icon-xiaoxi"></i><span>我的消息</span></li>');
                html.push('</ul>');
                html.push('</div>');
            }
            //跳转连接
            html.push('<div class="wdshheng">');
            html.push('<p id="myInvite" class="mkbiaoti mbtopbottom  wdshht"><em class="wdshtb"><b><img src="../../images/wdshtb3.png"></b>我的邀请</em><span class="wdshyb"><i class="iconfont icon-jiantou2"></i></span></p>');
            html.push('<p id="myIncome" class="mkbiaoti mbtopbottom  wdshht"><em class="wdshtb"><b><img src="../../images/wdshtb4.png"></b>我的收入</em><span class="wdshyb"><i class="iconfont icon-jiantou2"></i></span></p>');
            html.push('<p id="accountSafe" class="mkbiaoti mbtopbottom  wdshht"><em class="wdshtb"><b><img src="../../images/wdshtb5.png"></b>账户与安全</em><span class="wdshyb"><i class="iconfont icon-jiantou2"></i></span></p>');
            html.push('<p id="feedback" class="mkbiaoti mbtopbottom  wdshht"><em class="wdshtb"><b><img src="../../images/wdshtb6.png"></b>意见反馈</em><span class="wdshyb"><i class="iconfont icon-jiantou2"></i></span></p>');
            html.push('</div>');

            $("#main").html(html.join(''));
        },
        //绑定事件
        bindEvent: function (data) {

            $("#main").scroll(function () {
              var top = $(this).scrollTop();
              console.log(top);
              $("#header").css({"background": "rgba(240, 130, 0, " + (top / 118) + ")"});
            });

            if (data && data.member_id > 0) {
                //顶部设置
                $("#sAccount").on("click", function () {
                    redirect("account_safe.html");
                    return false;
                });
                //上传星粉秀
                $("#uploadShow").on("click", function () {
                    redirect("showpublish.html");
                    return false;
                });
                //个人图像
                $("#avatar").on("click", "#bacode", function () {
                    redirect("../account/barcode.html");
                    return false;
                });
                //昵称
                $("body").on("click", "#nickname", function () {
                    redirect("../account/account.html");
                    return false;
                });
                //关注的宝贝
                $("body").on("click", "#focus_goods", function () {
                    redirect("../mine/attention.html");
                    return false;
                });
                //关注的商家
                $("body").on("click", "#focus_shop", function () {
                    redirect("../mine/attention.html");
                    return false;
                });
                //足迹
                $("body").on("click", "#trace", function () {
                    redirect("../mine/attention.html");
                    return false;
                });

                //订单
                $("body").on("click", "#orders", function () {
                    redirect("../order/orders.html");
                    return false;
                });
                //订单分类
                $("body").on("click", "li[name='liorders']", function () {
                    var tip = $(this).attr('tip');
                    redirect("../order/orders.html");
                    return false;
                });

                //领星币
                $("body").on("click", "#coinlist", function () {
                    redirect("../coin/coinlist.html");
                    return false;
                });
                //我的星粉秀
                $("body").on("click", "#myShow", function () {
                    redirect("show.html");
                    return false;
                });
                //我的优惠券
                $("body").on("click", "#myCoupon", function () {
                    redirect("coupon.html");
                    return false;
                });
                //我的消息
                $("body").on("click", "#myMsg", function () {
                    redirect("message.html");
                    return false;
                });

                //我的邀请
                $("body").on("click", "#myInvite", function () {
                    redirect("invite.html");
                    return false;
                });
                //我的收入
                $("body").on("click", "#myIncome", function () {
                    redirect("income.html");
                    return false;
                });
                //账户与安全
                $("body").on("click", "#accountSafe", function () {
                    redirect("account_safe.html");
                    return false;
                    //redirect("../account/account.html");
                });
                //意见反馈
                $("body").on("click", "#feedback", function () {
                    redirect("../mine/feedback.html");
                    return false;

                });
            } else {
                $("body").on("click", "#avatar,#sAccount,#bacode,#nickname,#focus_goods,#focus_shop,#trace,#orders,li[name='liorders'],#coinlist,#myShow,#myCoupon,#myMsg,#myInvite,#myIncome,#accountSafe,#feedback", function () {
                    redirect("../account/login.html");
                    return false;
                });
            }

        }
    }

    _thisPage = new mylifePage();
}





